<?php

namespace App\Transformers;

use App\Models\Pictuer;
use League\Fractal\TransformerAbstract;
use Auth;
class PictuerTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['tags'];

    public function transform(Pictuer $pictuer)
    {
        if( !$user = Auth::user()) {
          $liked = false;
        } else {
          $liked = $pictuer->isLikedBy($user);
        }
        return [
            'id' => $pictuer->id,
            'title' => $pictuer->title,
            'view' => $pictuer->view_counts,
            'up' => $pictuer->up_counts,
            'path' => $pictuer->path,
            'filename' => $pictuer->filename,
            'width' => $pictuer->width,
            'height' => $pictuer->height,
            'url' => $pictuer->true_url,
            'src' => $pictuer->true_url,
            'w' => $pictuer->width,
            'h' => $pictuer->height,
            'liked' => $liked,
            'like_count' => $pictuer->likers()->count(),
            //'checked' => false,
            'created_at' => $pictuer->created_at ? $pictuer->created_at->diffForHumans() : now()->diffForHumans(),
            'updated_at' => $pictuer->updated_at ? $pictuer->updated_at->diffForHumans() : now()->diffForHumans(),
        ];
    }

    public function includeTags(Pictuer $pictuer)
    {
        return $this->collection($pictuer->tags, new TagTransformer());
    }
}
